package com.wind.stream;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Stream的使用：分组
 *
 * @author Wind
 * @date 2021-06-09
 */
public class Test09Stream {
    public static void main(String[] args) {

        List<Person> persons = new ArrayList<>();
        persons.add(new Person("张三", 7000, 34, "男", "陕西"));
        persons.add(new Person("李四", 8000, 24, "男", "河北"));
        persons.add(new Person("王五", 6000, 42, "女", "甘肃"));
        persons.add(new Person("王五", 9000, 12, "女", "上海"));

       // 分区：将员工薪资按照是否高于7000分区
        Map<Boolean, List<Person>> partitioning = persons.stream().collect(Collectors.partitioningBy(x -> x.getSalary() > 7000));
        System.out.println(partitioning);

        // 分组：将员工按照性别分组
        Map<String, List<Person>> grouping1 = persons.stream().collect(Collectors.groupingBy(Person::getSex));
        System.out.println(grouping1);
        // 分组：将员工先按照性别分组，再按照地区分组
        Map<String, Map<String, List<Person>>> grouping2 = persons.stream().collect(Collectors.groupingBy(Person::getSex, Collectors.groupingBy(Person::getArea)));
        System.out.println(grouping2);
    }
}
